package com.chuanyang.driverh.mainhome.fragment;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.IdRes;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.base.OnCustomListener;
import com.chuanyang.driverh.mainhome.FilterActivity;
import com.chuanyang.driverh.mainhome.adapter.WaitTransDriverAdapter;
import com.chuanyang.driverh.mainorder.SureUnLoadOverActivity;
import com.chuanyang.driverh.mainother.BeforeNaviActivity;
import com.chuanyang.driverh.response2.RequestFilter;
import com.chuanyang.driverh.response2.ResultWaitTransDriver;
import com.chuanyang.driverh.response2.ResultWaitTransTop;
import com.chuanyang.driverh.responseresult.OrderAddressResult;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.responseresult.ValidateCodeResult;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.EventBusUtil;
import com.chuanyang.driverh.utils.eventbus.FilterEvent;
import com.chuanyang.driverh.utils.eventbus.RefleshFragEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;



public class WaitTransDriverFragment extends BaseListFragment implements OnCustomListener {
    private WaitTransDriverAdapter listAdapter;
    private boolean validateOk;

    private View title_bar;
    private RadioGroup rg_top;
    private RadioButton rb0, rb1, rb2, rb3;
    private RequestFilter filter;
    private String showType = "1";//(1:全部计划，2:当日/月实发, 3:单日/月待发, 4:当日/月在运)
    private ArrayList<ResultWaitTransDriver.OutDataEntity.DataEntity.PageWayBillListEntity> billList = new ArrayList();
    private AlertDialog alertDialog;
    private EditText et_sureunload_code;
    private TextView tv_cancle_unload;
    private TextView tv_sureunload_getcode;
    private TextView tv_sure_unload;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1://分配的运单确认收货
                    updateWaybillUnload((String) msg.obj, true);
                    break;
                case 2://司机的个人订单确认收货
//                    updateWaybillUnload((String) msg.obj, false);
                    updteOrderUnload((String) msg.obj);
                    break;
            }
        }
    };
    private Dialog dialog;
    private ArrayList<File> files;
    @Override
    public BaseListAdapter createAdapter() {
        return listAdapter = new WaitTransDriverAdapter(getActivity());
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        title_bar = View.inflate(getContext(), R.layout.home_header_get_order, null);
        rg_top = (RadioGroup) title_bar.findViewById(R.id.rg_top);
        rb0 = (RadioButton) title_bar.findViewById(R.id.rb0);
        rb1 = (RadioButton) title_bar.findViewById(R.id.rb1);
        rb2 = (RadioButton) title_bar.findViewById(R.id.rb2);
        rb3 = (RadioButton) title_bar.findViewById(R.id.rb3);

        if (root.getChildCount() > 0) root.removeAllViews();
        root.addView(title_bar, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        listAdapter.setOnCustomListener(this);
    }

    @Override
    protected void initData() {
        filter = new RequestFilter();
   /*     if (!StringUtil.isBlank(SharedPrefUtil.getFleetId())) {
            rb0.setText( "全部计划(吨)" + "\n" +"0");
            rb1.setText("当日实发(吨)"+ "\n" +"0");
            rb2.setText("待发(吨)"+ "\n" +"0");
        } else {
            rb0.setText( "累计实发(吨)"+ "\n" +"0");
            rb1.setText( "当日实发(吨)"+ "\n" +"0");
            rb2.setText( "待发(吨)"+ "\n" +"0");
        }*/

        rb0.setText( "累计实发(吨)"+ "\n" +"0");
        rb1.setText( "当日实发(吨)"+ "\n" +"0");
        rb2.setText( "待发(吨)"+ "\n" +"0");
        rb3.setText("在运(吨)"+ "\n" +"0");


        onRefresh();
    }

    @Override
    protected void initListener() {
        rg_top.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb0:
                        showType = "1";
                        break;
                    case R.id.rb1:
                        showType = "2";
                        break;
                    case R.id.rb2:
                        showType = "3";
                        break;
                    case R.id.rb3:
                        showType = "4";
                        break;
                }
                filter = new RequestFilter();
                onRefresh();
            }
        });

    }





    private void getData() {
        DialogUtil.showLoadingDialog(getActivity(), "加载中...");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("driverId", SharedPrefUtil.getUserId());
        if (StringUtil.isBlank(SharedPrefUtil.getFleetId())) {
            params.put("type", "1");
        } else {
            params.put("type", "2");
        }

        params.put("page", mCurrentPage + "");
        params.put("length", "10");
//        params.put("showType", showType);

        if (!StringUtil.isBlank(filter.getStartPoint())) {
            params.put("startPoint", filter.getStartPoint());
        }
        if (!StringUtil.isBlank(filter.getEndPoint())) {
            params.put("endPoint", filter.getEndPoint());
        }
        if (!StringUtil.isBlank(filter.getStartTime())) {
            params.put("startTime", filter.getStartTime());
        }
        if (!StringUtil.isBlank(filter.getEndTime())) {
            params.put("endTime", filter.getEndTime());
        }
        //暂时去掉，不需要这个字段
        if (!StringUtil.isBlank(filter.getSelectType())) {
            params.put("selectType", filter.getSelectType());
        }

        if (filter.getOrderStatus2()!=null && filter.getOrderStatus2().size()>0 && filter.getOrderStatus2().size()!=4){
            params.put("orderStatues",StringUtil.getSplitString(filter.getOrderStatus2()));
        }
        if (filter.getOrderType2()!=null && filter.getOrderType2().size()>0){
            params.put("orderTypes",StringUtil.getSplitString(filter.getOrderType2()));
        }


        //当做筛选时，showtype 传0；
        if (params.size()>4){
            params.put("showType", 0);
        }else {
            params.put("showType", showType);
        }

        MyHttpUtil.waitTrans(getActivity(), params, new MyCallback<ResultWaitTransDriver>(getActivity(), new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                springView.onFinishFreshAndLoad();
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(ResultWaitTransDriver result, int id) {
                springView.onFinishFreshAndLoad();
                if (result.getCode() == ConstUtil.OK) {
                    try {
                        if (result.getData().getData().size() > 0) {
                            billList.clear();
                            for (int i = 0; i < result.getData().getData().size(); i++) {
                                ResultWaitTransDriver.OutDataEntity.DataEntity orderEntity = result.getData().getData().get(i);
                                for (int j = 0; j < orderEntity.getPageWayBillList().size(); j++) {
                                    ResultWaitTransDriver.OutDataEntity.DataEntity.PageWayBillListEntity billEntity = orderEntity.getPageWayBillList().get(j);
                                    billEntity.setDriverId(orderEntity.getDriverId());
                                    billEntity.setFleetId(orderEntity.getFleetId());
                                    billEntity.setOrderId(orderEntity.getOrderId());
                                    if (orderEntity.getCreateTime()!=null){
                                        billEntity.setOrderTime(orderEntity.getCreateTime());
                                    }else {
                                        billEntity.setOrderTime(orderEntity.getOrderTime());
                                    }
                                    billEntity.setOrderNo(orderEntity.getOrderNo());
                                    billEntity.setObjType(orderEntity.getOrderType());
                                    billEntity.setStartPoint(orderEntity.getStartPoint());
                                    billEntity.setEndPoint(orderEntity.getEndPoint());
                                    billEntity.setOrderType(orderEntity.getOrderType());
                                    billEntity.setCurrency(orderEntity.getCurrency());
                                    billEntity.setContactPhone(orderEntity.getPhoneCall());
                                    billEntity.setTransPortWay(orderEntity.getTransPortWay());
                                    billEntity.setOrderStatus(orderEntity.getOrderStatus());
                                    billEntity.setPrice(orderEntity.getPrice());
                                    billEntity.setSumNum(orderEntity.getPageWayBillList().get(j).getSumNum());
                                    billEntity.setSumWeight(orderEntity.getPageWayBillList().get(j).getSumWeight());
                                    billEntity.setOriginalNum(orderEntity.getPageWayBillList().get(j).getOriginalNum());
                                    billEntity.setOriginalWeight(orderEntity.getPageWayBillList().get(j).getOriginalWeight());
                                    billEntity.setApplyNoStatus(orderEntity.getPageWayBillList().get(j).getApplyNoStatus());
                                    billEntity.setNoticeNo(orderEntity.getPageWayBillList().get(j).getNoticeNo());
                                   if (orderEntity.getTransPortWay()==2){
                                       billEntity.setStevedoreDay(orderEntity.getStevedoreDay());
                                       billEntity.setFq(orderEntity.getFq());
                                       billEntity.setLoadDate(orderEntity.getLoadDate());
                                   }
                                    if (orderEntity.getPageWayBillList().size() > 1) {
                                        if (j == 0) {
                                            billEntity.setObjType(2);
                                        } else {
                                            billEntity.setObjType(3);
                                        }
                                    } else {
                                        billEntity.setObjType(1);
                                    }
                                    billList.add(billEntity);
                                }
                            }
                            setDataResult(billList);

                        }
//                        List<ResultWaitTransDriver.OutDataEntity.DataEntity> ss = result.getData().getData();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(), result.getMsg());
                }
            }
        });
    }

    private void getTopData() {
        DialogUtil.showLoadingDialog(getActivity(), "加载中...");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("type", "1");

        MyHttpUtil.waitTransTopInfo(getActivity(), params, new MyCallback<ResultWaitTransTop>(getActivity(), new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                springView.onFinishFreshAndLoad();
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(ResultWaitTransTop result, int id) {
                if (result.getCode() == ConstUtil.OK) {

                    try {
                        setHeader(result.getData());
                    } catch (Exception e) {
                        if (result.getMsg() != null) {
                            PrintUtil.toast(activityContext, result.getMsg());
                        }
                        listAdapter.clear();
                        listAdapter.notifyDataSetChanged();
                    }


                } else {
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(), result.getMsg());
                }
            }
        });
    }

    private void setHeader(ResultWaitTransTop.DataEntity dispatchInfo) {
        if (dispatchInfo == null) return;
  /*      if (!StringUtil.isBlank(SharedPrefUtil.getFleetId())) {
            rb0.setText(StringUtil.getWeight(dispatchInfo.getOrderSumSrplusWeight()) + "\n" + "全部计划(吨)");
            rb1.setText(StringUtil.getWeight(dispatchInfo.getCompleteSumDetailsWeightByTime()) + "\n" + "当日实发(吨)");
            rb2.setText(StringUtil.getWeight(dispatchInfo.getNotShipmentSumOriginalWeightByTime()) + "\n" + "待发(吨)");
        } else {
            rb0.setText(StringUtil.getWeight(dispatchInfo.getWaybillSumDetailsWeight()) + "\n" + "累计实发(吨)");
            rb1.setText(StringUtil.getWeight(dispatchInfo.getWaybillSumDetailsWeightByTime()) + "\n" + "当日实发(吨)");
            rb2.setText(StringUtil.getWeight(dispatchInfo.getNotShipmentSumOriginalWeightByTime()) + "\n" + "待发(吨)");
        }*/


        rb0.setText("累计实发(吨)"+ "\n" + StringUtil.getWeight(dispatchInfo.getWaybillSumDetailsWeight()) );
        rb1.setText( "当日实发(吨)"+ "\n" +StringUtil.getWeight(dispatchInfo.getWaybillSumDetailsWeightByTime()) );
        rb2.setText(  "待发(吨)"+ "\n" +StringUtil.getWeight(dispatchInfo.getNotShipmentSumOriginalWeightByTime()));
        rb3.setText( "在运(吨)"+ "\n" + StringUtil.getWeight(dispatchInfo.getTransportSumDetailsWeight()));
    }

    @Override
    public void onGetMore() {
        getData();
    }

    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
        getTopData();
    }

    @Override
    protected void onForceRefresh() {
        onRefresh();
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    @Override
    public void onCustomerListener(View v, int position) {
        final ResultWaitTransDriver.OutDataEntity.DataEntity.PageWayBillListEntity databean = listAdapter.getItem(position);
        int waybillstatus = databean.getWaybillStatus();
        switch (v.getId()) {
            case R.id.tv_act1:
                if (waybillstatus == 2)//申请放弃
                {
                    dialog = DialogUtil.myDialog(getActivity(), "提示", "取消", "确定", "您是否申请放弃运单?", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            switch (v.getId()) {
                                case R.id.dialog_btn_left:
                                    break;
                                case R.id.dialog_btn_right:
                                    applyNoWaybill(databean.getWaybillId() + "");
                                    break;
                            }
                            dialog.cancel();
                        }
                    });
                } else if (waybillstatus == 3)//装货向导
                {
                    PrintUtil.toast(getActivity(), "装货向导");

                } else if (waybillstatus == 4)//导航
                {
                    selectOrderAddress(databean.getOrderId() + "");

                } else if (waybillstatus == 5)//卸货向导
                {
                    PrintUtil.toast(getActivity(), "卸货向导");

                } else if (waybillstatus == 6)//验证卸货
                {
                    if (databean.getFleetId() != 0) {
                        sureAcceptGood(databean.getContactPhone(), databean.getWaybillId() + "", true);
                    } else {
                        sureAcceptGood(databean.getContactPhone(), databean.getOrderId() + "", false);
                    }
                }
                break;
            case R.id.tv_act2:
                if (waybillstatus == 2)//开始运输
                {
                    startTranslate(databean);
                }
                if (waybillstatus == 4)//确认卸货
                {
                    Intent intent = new Intent(getActivity(), SureUnLoadOverActivity.class);
                    if (databean.getFleetId() == 0) {
                        intent.putExtra("orederId", databean.getOrderId());
                        intent.putExtra("fromlist", true);
                        intent.putExtra("personalOrder", true);
                    } else {
                        intent.putExtra("waybillId", databean.getWaybillId());
                        intent.putExtra("fromlist", true);
                        intent.putExtra("personalOrder", false);
                    }


                    startActivity(intent);
                }
                if (waybillstatus == 5)//确认卸货
                {
                    Intent intent = new Intent(getActivity(), SureUnLoadOverActivity.class);
                    if (databean.getFleetId() == 0) {
                        intent.putExtra("orederId", databean.getOrderId());
                        intent.putExtra("fromlist", true);
                        intent.putExtra("personalOrder", true);
                    } else {
                        intent.putExtra("waybillId", databean.getWaybillId());
                        intent.putExtra("fromlist", true);
                        intent.putExtra("personalOrder", false);
                    }

                    startActivity(intent);
                }
                break;
        }
    }

    /**
     * 开始运输
     */
    private void startTranslate(ResultWaitTransDriver.OutDataEntity.DataEntity.PageWayBillListEntity order) {
        DialogUtil.showLoadingDialog(getActivity(), "请求中...");
        Map<String, String> params = new HashMap<String, String>();
        params.put("waybillId", order.getWaybillId() + "");
        params.put("waybillStatus", "4");
        params.put("driverId", SharedPrefUtil.getUserId());
        MyHttpUtil.startTranslate(getActivity(), params, new MyCallback<Result>(getActivity(), new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                e.printStackTrace();
            }

            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    mCurrentPage = 1;
                    getData();
                } else {
                    PrintUtil.toast(getActivity(), result.getMsg());
                }
            }
        });


    }

    /**
     * 导航请求数据
     *
     * @param id
     */
    private void selectOrderAddress(String id) {
        getEmptyView().setVisibility(View.GONE);
        DialogUtil.showLoadingDialog(getActivity(), "请求中...");
        Map<String, String> params = new HashMap<String, String>();
        params.put("orderId", id);
        MyHttpUtil.selectOrderAddress(getActivity(), params, new MyCallback<OrderAddressResult>(getActivity(), new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                e.printStackTrace();
            }

            @Override
            public void onSuccess(OrderAddressResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    Intent intent = new Intent(getActivity(), BeforeNaviActivity.class);
                    intent.putExtra("OrderAddressResult", result);
                    startActivity(intent);
                } else {
                    PrintUtil.toast(getActivity(), result.getMsg());
                }
            }
        });
    }

    /**
     * 司机申请拒绝接受运单
     *
     * @param id
     */
    private void applyNoWaybill(String id) {
        getEmptyView().setVisibility(View.GONE);
        DialogUtil.showLoadingDialog(getActivity(), "请求中...");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("waybillId", id);
        MyHttpUtil.applyNoWaybill(getActivity(), params, new MyCallback<Result>(getActivity(), new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                e.printStackTrace();
            }

            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {

                    mCurrentPage = 1;
                    getData();
                } else {
                    PrintUtil.toast(getActivity(), result.getMsg());
                }
            }
        });
    }

    /**
     * 验证卸货
     *
     * @param phone id
     */
    public void sureAcceptGood(final String phone, final String id, final boolean iswaybill) {

        if (null == alertDialog) {
            alertDialog = new AlertDialog.Builder(getActivity()).create();
            alertDialog.show();
            alertDialog.setTitle(null);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

                @Override
                public void onDismiss(DialogInterface dialog) {
                    // TODO Auto-generated method stub
                    alertDialog = null;
                }
            });
            Window window = alertDialog.getWindow();
            window.setGravity(Gravity.CENTER);
            window.setBackgroundDrawable(new ColorDrawable(0));
            window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            window.setContentView(R.layout.dialog_sureunload);
            View view = window.getDecorView();
            et_sureunload_code = (EditText) view.findViewById(R.id.et_sureunload_code);
            tv_cancle_unload = (TextView) view.findViewById(R.id.tv_cancle_unload);
            tv_sureunload_getcode = (TextView) view.findViewById(R.id.tv_sureunload_getcode);
            tv_sure_unload = (TextView) view.findViewById(R.id.tv_sure_unload);
            alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    validateOk = false;

                }
            });
            tv_cancle_unload.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                    alertDialog = null;
                }
            });
            tv_sureunload_getcode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!validateOk) {//获取成功验证码 且对话框没消失时无法多次点击
                        getValidateCode(phone, id);
                    } else {

                        PrintUtil.toast(getActivity(), "验证码已发送给货主！");
                    }
                }
            });
            tv_sure_unload.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (TextUtils.isEmpty(et_sureunload_code.getText().toString().trim())) {
                        PrintUtil.toast(getActivity(), "请填写获取到验证码");
                        return;
                    }
                    if (!validateOk){
                        PrintUtil.toast(activityContext,"请先获取验证码");
                        return;
                    }

                    Message message = Message.obtain();
                    message.obj = id;
                    if (iswaybill) {
                        message.what = 1;

                    } else {
                        message.what = 2;

                    }
                    mHandler.sendMessage(message);
//                    alertDialog.dismiss();
//                    alertDialog = null;
                }
            });
        }
        //下面两行代码加入后即可弹出输入法
        alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }

    /**
     * 获取验证码
     */
    private void getValidateCode(String phone, String id) {
        DialogUtil.showLoadingDialog(getActivity(), "加载中...");
        Map<String, String> params = new HashMap<String, String>();
        params.put("phone", phone);
        params.put("validateMsg", id);
        MyHttpUtil.getSureValidateCode(getActivity(), params, new MyCallback<ValidateCodeResult>(getActivity(), new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(ValidateCodeResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    validateOk = true;
                    tv_sureunload_getcode.setBackgroundDrawable(getResources().getDrawable(R.drawable.tv_shape_gray_round_corner_solid));
                } else {
                    PrintUtil.toast(getActivity(), result.getMsg());
                }
            }
        });


    }

    /**
     * 收货完成 （传入订单id或者运单id）
     */
    private void updateWaybillUnload(final  String waybilOrOrderId, final boolean iswaybill) {
        DialogUtil.showLoadingDialog(getActivity(), "加载中...");
        Map<String, String> params = new HashMap<String, String>();
        if (iswaybill) {
            params.put("waybillId", waybilOrOrderId);
        } else {
            params.put("orderId", waybilOrOrderId);
        }
        params.put("waybillStatus", "7");
        params.put("validateMsg", waybilOrOrderId + "");
        params.put("validateCode", et_sureunload_code.getText().toString().trim());
        MyHttpUtil.updateWaybillStatus(getActivity(), params, new MyCallback<Result>(getActivity(), new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(getActivity(), "您已替货主确认收货");
//                    mCurrentPage = 1;
//                    getData();

                    alertDialog.dismiss();
                    alertDialog = null;

                    onRefresh();

                } else {
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }

    /**
     * 代替货主收货接口 (个人订单时)
     */
    private void updteOrderUnload(String orderId) {
        DialogUtil.showLoadingDialog(activityContext,"请求中...");
        Map<String,String> params=new HashMap<String, String>();
        if (files == null) {
            files = new ArrayList();
        }
        params.clear();
        params.put("orderId", orderId + "");
        params.put("orderStatus", "7");
        params.put("validateMsg", orderId + "");
        params.put("validateCode", et_sureunload_code.getText().toString().trim());
        MyHttpUtil.updateOrderUnload(activityContext,files,params, new MyCallback<Result>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(activityContext, "您已替货主确认收货");
                    EventBusUtil.postEvent(new RefleshFragEvent(3));
                    alertDialog.dismiss();
                    alertDialog = null;
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }


    /**
     * 收货完成 （传入订单id或者运单id）
     */
 /*   private void shouhuo(String id) {//type0运单，1订单
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
            params.put("orderId", id);
            params.put("orderStatus", "7");
        MyHttpUtil.signoff(activityContext,params, new MyCallback<Result>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    onRefresh();
                    PrintUtil.toast(activityContext,"您已替货主确认收货");
                    EventBusUtil.postEvent(new RefleshFragEvent(3));
                }else{
//                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });


    }*/

    public void onEventMainThread(RefleshFragEvent event) {
        if (event.getIndex()==3) {
            onRefresh();
        }
    }

    public void filter() {
        Intent intent = new Intent(activityContext, FilterActivity.class);
        intent.putExtra("type", "3");
        intent.putExtra("data", filter);
        startActivity(intent);
    }

    public void onEventMainThread(FilterEvent event) {
        if ("3".equals(event.getType()) ){
            filter=event.getData();
            onRefresh();
        }
    }

}
